package face.pay.web.com.applyment;

import face.pay.common.enterprise.service.IAlipayMachInfoService;
import face.pay.common.enterprise.service.IBaseMchInfoService;
import face.pay.common.enterprise.service.IMchChannelService;
import face.pay.entity.UserInfo;
import face.pay.system.annotation.CurrentUser;
import face.pay.util.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 进件首页
 */
@Controller
@RequestMapping("/applyment")
public class ApplyMentIndexController {

    @Autowired
    private IMchChannelService iMchChannelService;

    @Autowired
    private IBaseMchInfoService baseMchInfoService;

    @Autowired
    private IAlipayMachInfoService iAlipayMachInfoService;


    @RequestMapping("/index")
    public String index(@CurrentUser UserInfo loginUser, Integer mchUserId, Model model) {

        mchUserId = mchUserId == null ? loginUser.getId() : mchUserId;

        Map<String, Boolean> channelMap = iMchChannelService.getUserChannels(mchUserId);

        model.addAttribute("mchUserId", mchUserId);

        model.addAttribute("channels", channelMap);
        /**
         * 判断当前用户 所支持的进件渠道
         */
        return "agency/enterprise/applyment/applyment";
    }


    /**
     * 文件上传
     *
     * @param file
     * @param loginUser
     * @return
     */
    @RequestMapping("/fileUpload")
    @ResponseBody
    public ResultMessage fielUpload(@RequestParam("file") MultipartFile file, String keyName, Integer userId, @CurrentUser UserInfo loginUser) throws Exception {
        byte[] fileBytes = file.getBytes();
        String originalFilename = file.getOriginalFilename();
        return this.iAlipayMachInfoService.fielUpload(fileBytes, originalFilename, keyName, userId, loginUser);
    }

    /**
     * 获取文件
     *
     * @param loginUser
     * @param fielId
     * @return
     */
    @RequestMapping(value = "/getFile", produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public byte[] getFiel(@CurrentUser UserInfo loginUser, @RequestParam String fielId) {
        return baseMchInfoService.getMedia(fielId, loginUser);
    }
}
